import sys
import yaml
from termcolor import colored

from trees import MagicMap, MagicList
from context import Context, Reference
from scheduler import SimpleScheduler, RandomScheduler
from object import *

def load(filename):
  f=open(filename, 'r')
  y=yaml.load(f)
  f.close()

#  mm=MagicMap(map=y)
#  return mm

  return y

def runTest(test):
  sched=SimpleScheduler()
#  args=MagicList()
  args=[]
  t=test.call(sched, args)
  sched.run()

  if t.error:
    print 'Error:', colored(t.errmsg, 'red')
  else:
    print 'Output:', colored(t.exitValue, 'magenta')
    correct=test.get('correct')
    print 'Correct:', colored(correct, 'magenta')
    if t.exitValue==correct:
      print colored('Pass', 'green')
    else:
      print colored('Fail', 'red')

if __name__=='__main__':
  universe=ArbreUniverse(load('system2.yaml'))

  testSuiteFilename=sys.argv[1]
  testSuiteName=testSuiteFilename.split('.')[0]
  testSuite=load(testSuiteFilename)

  testMod=universe.addModule(testSuiteName, testSuite)
  universe.printTree(0)

  print 'testMod:', testMod, testMod.get('active')
  for testSet in testMod.get('active').value:
    testCls=testMod.get(testSet.value)
    print 'testCls:', testSet, testCls
    testObj=testCls.new('test', {})
    print 'testObj: ', testObj, testObj.get('active')
    for testMethod in testObj.get('active').value:
      test=testObj.get(testMethod.value)
      print 'test:', str(test)
      runTest(test)
